package com.weitu.workflow.definition.util;

import com.weitu.permission.user.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.fireflow.engine.definition.WorkflowDefinition;

/**
 * 工作流session管理
 * Created by Administrator on 2017/6/8.
 */
public class WorkFlowSession {
    /***
     * 将流程定义放入session中
     * @param workflowDefinition
     */
    public static void setWorkflowDefinition(WorkflowDefinition workflowDefinition){
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute(workflowDefinition.getProcessId(),workflowDefinition);
    }

    /***
     * 从session中获取流程定义
     * @return
     */
    public static WorkflowDefinition getWorkflowDefinition(String processId){
        Session session = SecurityUtils.getSubject().getSession();
        return session.getAttribute(processId)!=null?(WorkflowDefinition) session.getAttribute(processId):null;
    }
    /***
     * 从session中删除流程定义
     * @return
     */
    public static void deleteWorkflowDefinition(String processId){
        Session session = SecurityUtils.getSubject().getSession();
        session.removeAttribute(processId);
    }

    /***
     * 从session中获取用户
     * @return
     */
    public static User getCurrentUser(){
        Session session = SecurityUtils.getSubject().getSession();
        Object userObject=session.getAttribute("loginUser");
        if (userObject!=null){
           return  ((User)userObject);
        }
        return null;
    }
}
